package com.sad.jetpack.v1.datamodel.api;

import java.util.LinkedHashMap;

public class DataModelProduceFutureProvider {

    private final static LinkedHashMap<String,IDataModelProduceFuture> produceFutureRepository=new LinkedHashMap<>();

    protected static <RP> void register(String tag,IDataModelProduceFuture<RP> future){
        produceFutureRepository.put(tag,future);
    }

    protected static <RP> void register(IDataModelProduceFuture<RP> future){
        IDataModelRequest request=future.getRequest();
        if (request!=null){
            register(request.tag(),future);
        }

    }

    public static <RP> IDataModelProduceFuture<RP> unregister(String tag){
        return produceFutureRepository.remove(tag);
    }

    public static void clear(){
        produceFutureRepository.clear();
    }
    public static <RP> IDataModelProduceFuture<RP> get(String tag){
        return produceFutureRepository.get(tag);
    }

    public static <RP> IDataModelProduceFuture<RP> require(String tag){
        IDataModelProduceFuture<RP> future=get(tag);
        if (future==null){
            future=new InternalDataModelProduceFuture<>(DataModelRequestImpl.newCreator().tag(tag).create(), null);
            register(future);
        }
        return future;
    }
}
